﻿using System.Reflection;

// namespace ConsoleApp1.advance;


[AttributeUsage(AttributeTargets.All)]
public class HelpAttribute : Attribute
{
    public readonly string Url;

    public HelpAttribute(string url) // url 是一个定位（positional）参数
    {
        Url = url;
    }

    public string Topic // Topic 是一个命名（named）参数
    {
        get;
        set;
    }
}

[HelpAttribute("Information on the class MyClass")]
internal class MyClass
{
}

namespace AttributeAppl
{
    internal class Program
    {
        public static void TestMain()
        {
            Type info = typeof(MyClass); // 直接使用 Type 类型
            object[] attributes = info.GetCustomAttributes(true);
            for (int i = 0; i < attributes.Length; i++)
            {
                // 具体检查 HelpAttribute 并输出其属性值
                if (attributes[i] is HelpAttribute helpAttr)
                {
                    Console.WriteLine("Url: " + helpAttr.Url);
                    Console.WriteLine("Topic: " + (helpAttr.Topic ?? "null")); // 处理 Topic 为 null 的情况
                }
                else
                {
                    Console.WriteLine(attributes[i]); // 输出其他特性
                }
            }
            Console.ReadKey();
        }
    }
}